home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1996 September / MACPOWER-1996-09.ISO.7z / MACPOWER-1996-09.ISO / 第2特集:プラグイン大集合 / PIC2 Save / PIC2Main.c < prev    next >
Text File  |  1995-01-16  |  9KB  |  440 lines

  1. /*
  2.     File: PIC2Main.c
  3.  
  4.     Copyright © 1995 K&M Software Corporation (番犬 JBE01713@niftyserve.or.jp)
  5.     Portions  © 1992 Adobe Systems Incorporated
  6.                      Apple Computer, Inc.
  7.                      Free Software Foundation, Inc.
  8.     Pi Format ©         やなぎさわ
  9.  
  10. Version History:
  11.  1.0    First Create!
  12.  
  13. */
  14.  
  15. /*****************************************************************************/
  16. /* definitions */
  17.  
  18. #ifdef __MWERKS__
  19. #include <A4Stuff.h>
  20. #endif
  21.  
  22. #include "pic2.h"
  23. #include "PIC2PlugIn.h"
  24.  
  25. /*****************************************************************************/
  26.  
  27. /* Sets the global variables to their default values. */
  28.  
  29. void InitGlobals(GHdl globals)
  30. {
  31.     // Clear globals
  32.     memset((Ptr) *globals, 0, sizeof(Globals));
  33. }
  34.  
  35.  
  36. /*****************************************************************************/
  37.  
  38. /* Prepare to acquire an image.  If the plug-in module needs only a limited
  39.    amount of memory, it can lower the value of the 'maxData' field. */
  40.  
  41. void DoPrepare(GHdl globals)
  42. {
  43.     gResult = noErr;
  44.  
  45.     gStuff->maxData = gStuff->maxData*2/3L;
  46. #if 0
  47.     if (gStuff->maxData > 1524L*1024L)    /* 1.5MByte */    
  48.         gStuff->maxData = 1524L*1024L;    /* 1.5MByte */
  49.     else
  50.         gResult = memFullErr;        // Sorry..
  51. #endif
  52. }
  53.  
  54. /*****************************************************************************/
  55.  
  56. #define    kOKOutlineItem        3
  57. #define k24bitColor            4
  58. #define    k15bitColor            5
  59. #define    kBetaMode            6
  60. #define    kFastMode            7
  61. #define    kCompressMode        8
  62. #define    kTitleStr            9
  63. #define    kCommentStr            10
  64. #define    kAuthorStr            11
  65. #define    kPicIndexStr        12
  66.  
  67. #define    inkey(keys,mask)    ( ((Ptr)keys)[(mask>>3)] & (1<<(mask&7)) )
  68.  
  69. Boolean GetParameters(GHdl globals)
  70. {
  71.     short        i,item,itemType;
  72.     Rect        r;
  73.     Point        pt;
  74.     SFReply        reply;
  75.     ControlHandle    ch1[8];
  76.     Handle        strs[4];
  77.     Str255        str,str1;
  78.     DialogTHndl    dt;
  79.     DialogPtr    dp;
  80.     
  81.     (**globals).p2 = nil;
  82.     SetPt(&pt,100,100);
  83.     (**globals).compressMode = 2;
  84.     dt = (DialogTHndl) GetResource ('DLOG', kMainDialogID);
  85.     HNoPurge ((Handle) dt);
  86.     CenterDialog (dt);
  87.     dp = GetNewDialog (kMainDialogID, nil, (WindowPtr) -1);
  88.     ShowWindow(dp);
  89.     SetPort(dp);
  90.     SetOutlineOKHook(dp, kOKOutlineItem);
  91.     ParamText((StringPtr)cPIC2SaveVersion,(StringPtr)nil,
  92.         (StringPtr)nil,(StringPtr)nil);
  93.     SelectDialogItemText(dp, kTitleStr, 0, 255);
  94.  
  95.     for(i=0;i<2;i++)
  96.     {
  97.         GetDialogItem (dp, k24bitColor+i, &itemType, (Handle *) &ch1[i], &r);
  98.         if(gStuff->imageMode == exportModeIndexedColor)
  99.             HiliteControl(ch1[i], 255);        // inactive state
  100.         else
  101.             HiliteControl(ch1[i], 0);
  102.     }
  103.     for(i=0;i<3;i++)
  104.         GetDialogItem (dp, kBetaMode+i, &itemType, (Handle *) &ch1[i+2], &r);
  105.     for(i=0;i<4;i++)
  106.         GetDialogItem (dp, kTitleStr+i, &itemType, (Handle *) &strs[i], &r);
  107.  
  108.     do
  109.     {
  110.         if(gStuff->imageMode != exportModeIndexedColor)
  111.         {
  112.             for(i=0;i<2;i++)
  113.                 SetControlValue (ch1[i], (**globals).colorMode==i);
  114.         }
  115.         for(i=0;i<3;i++)
  116.             SetControlValue (ch1[i+2], (**globals).compressMode==i);
  117.  
  118.         ModalDialog (nil, &item);
  119.         switch(item)
  120.         {
  121.         case k24bitColor:
  122.         case k24bitColor+1:
  123.             (**globals).colorMode = item-k24bitColor;
  124.             break;
  125.         case kBetaMode:
  126.         case kBetaMode+1:
  127.         case kBetaMode+2:
  128.             (**globals).compressMode = item-kBetaMode;
  129.             break;
  130.         }
  131.     }
  132.     while (item != ok && item != cancel);
  133.  
  134.     if(item==ok)
  135.     {
  136.         Str255 p;
  137.         long    count;
  138.  
  139.         // Dialogs.h
  140.         GetDialogItemText(strs[0],p);
  141.         if(StrLength(p)<30)
  142.         {
  143.             BlockMove(&p[1],(**globals).title,StrLength(p));
  144.             (**globals).title[StrLength(p)]=0;
  145.         }
  146.         
  147.         GetDialogItemText(strs[1],p);
  148.         BlockMove(&p[1],(**globals).comment,StrLength(p));
  149.         (**globals).comment[StrLength(p)]=0;
  150.  
  151.         GetDialogItemText(strs[2],p);
  152.         if(StrLength(p)<17)
  153.         {
  154.             BlockMove(&p[1],(**globals).author,StrLength(p));
  155.             (**globals).author[StrLength(p)]=0;
  156.         }
  157.  
  158.         GetDialogItemText(strs[3],p);
  159.         StringToNum(p, &count);
  160.         (**globals).picindex = count;
  161.     }
  162.     
  163.     DisposDialog (dp);
  164.     HPurge ((Handle) dt);
  165.  
  166.     if(item == cancel)
  167.         return    FALSE;
  168.  
  169.     GetIndString(str, kStringID, kStrSavePromptItem);
  170.  
  171.     BlockMove(gStuff->filename,str1,250);
  172.     str1[1+str1[0]]='.';
  173.     str1[0]++;
  174.     for(item=1;item<=str1[0];item++)
  175.         if(str1[item]=='.')    // not appear in SJIS-2byte
  176.         {    BlockMove(".p2",&str1[item],4);
  177.             break;
  178.         }
  179.     for(item=0;str1[1+item]!=0 && item<30;) item++;
  180.     str1[0]=item;
  181.     
  182.     SFPutFile(pt,str,str1,nil,&reply);
  183.  
  184.     if(!reply.good)    return FALSE;
  185.     
  186.     BlockMove(reply.fName,gStuff->filename,reply.fName[0]+1);
  187.     gStuff->vRefNum = reply.vRefNum;
  188.     gStuff->dirty = FALSE;
  189.     ReplyToSpec(&(**globals).theSpec, &reply);
  190.     return    TRUE;
  191. }
  192.  
  193. #undef    kMainDialogID
  194.  
  195. /*****************************************************************************/
  196.  
  197. /* Asks the user and the returns to the calling program the image parmaters. */
  198.  
  199. void    DoStart(GHdl globals)
  200. {
  201.     P2*        p2;
  202.  
  203.     if(gResult != noErr)    return;
  204.     if (gStuff->imageMode != exportModeRGBColor && gStuff->imageMode != exportModeIndexedColor)
  205.     {
  206.         gResult = exportBadMode;
  207.         return;
  208.     }
  209.  
  210.     if (!GetParameters(globals))
  211.     {
  212. errorexit:;
  213.         gResult = 1;
  214.         return;
  215.     }
  216.  
  217.     {
  218.         Str255 str;
  219.         short    c;
  220.         GetIndString(str, kStringID, kStrSaverItem);
  221.         str[1+str[0]]=0;
  222.         if(gStuff->imageMode == exportModeRGBColor)
  223.             c = ((**globals).colorMode==1)? 15 : 24;
  224.         else
  225.             c = 8;
  226.         (**globals).BitsDepth = c;
  227.  
  228.         p2setenv((char*)&str[1], c, -1, -1, 1, 1, 0);
  229.         if(c==8)
  230.         {
  231.             uchar    pal[256][3];
  232.             short    i;
  233.             for(i=0;i<256;i++)
  234.             {
  235.                 pal[i][0] = gStuff->redLUT[i];
  236.                 pal[i][1] = gStuff->greenLUT[i];
  237.                 pal[i][2] = gStuff->blueLUT[i];
  238.             }
  239.             p2setpal(256,8,pal);
  240.         }
  241.         p2 = p2creat(&(**globals).theSpec, (**globals).title, (**globals).comment,
  242.                          (**globals).author, (**globals).picindex);
  243.         if(p2==nil)
  244.         {
  245.             gResult = DoP2Error(globals);
  246.             return;
  247.         }
  248.     }
  249.  
  250.     gResult = noErr;
  251.     
  252.     {
  253.         char    mode[20];
  254.  
  255.         // Create Additional Tag: 'PDPI'
  256.         write_file(p2, "PDPI", 4);
  257.         write_long(p2, 10);                // 10byte
  258.         write_short(p2, FixRound(gStuff->imageHRes));
  259.         
  260.         /* 次にセーブするブロック位置をせっと */
  261.         p2->next_pos = tell_file( p2);
  262.     
  263.  
  264.         mode[4]=0;
  265.         mode[0]='P';
  266.         mode[1]='2';
  267.         (**globals).partial = 0;
  268.         (**globals).total = gStuff->imageSize.v;
  269.         switch((**globals).compressMode)
  270.         {
  271.         case 0:
  272.             mode[2]='B';
  273.             mode[3]='M';
  274.             break;
  275.         case 1:
  276.             mode[2]='S';
  277.             mode[3]='F';
  278.             break;
  279.         default:
  280.             mode[2]='S';
  281.             mode[3]='S';
  282.             (**globals).total *= 2;
  283.             break;
  284.         }
  285.     
  286.         (**globals).cLine = p2save(p2, &(**globals).Pixel,
  287.             0, 0,
  288.             gStuff->imageSize.h, gStuff->imageSize.v,
  289.             mode, 0xffffffffL);
  290.     }
  291.     
  292.     (**globals).p2 = p2;
  293.  
  294.     (**globals).buffer_end = FALSE;
  295.  
  296.     gStuff->theRect.left  = 0;
  297.     gStuff->theRect.right = gStuff->imageSize.h;
  298.     gStuff->theRect.top   = (**globals).cLine;
  299.     gStuff->theRect.bottom = (**globals).cLine+1;
  300.     gStuff->loPlane = 0;
  301.     gStuff->hiPlane = gStuff->planes - 1;    // 3 planes
  302.     gStuff->dirty = FALSE;
  303.  
  304. }
  305.  
  306. /*****************************************************************************/
  307.  
  308. /* Returns the next hunk of the image to the calling program. */
  309.  
  310. void    DoContinue(GHdl globals)
  311. {
  312.     short    cLine;
  313.     register Byte* p;
  314.     register pix *col;
  315.     short    i,width;
  316.     /* Since Photoshop will continue to call DoContinue until it
  317.        returns error or data=NIL, we have to tell him its end. */
  318.  
  319.     if(gResult || (**globals).buffer_end)
  320.     {
  321. eexit:;
  322.         gStuff->data = NULL;
  323.         return;
  324.     }
  325.     
  326.     width = gStuff->imageSize.h;
  327.     col = (**globals).Pixel;
  328.     cLine = (**globals).cLine;
  329.     p = (Byte*) (gStuff->data);
  330.  
  331.     if((cLine & 63)==0)
  332.     {
  333.         if(TestAbort(globals))
  334.         {    p2close((**globals).p2);
  335.             (**globals).p2 = nil;
  336.             gResult=1;
  337.             goto eexit;
  338.         }
  339.         UpdateProgress(globals,(**globals).cLine+(**globals).partial,(**globals).total);
  340.     }
  341.     
  342.     switch((**globals).BitsDepth)
  343.     {
  344.     case 8:
  345.         for(i=0;i<width;i++)
  346.             *col++ = *p++;
  347.         break;
  348.     case 15:
  349.         for(i=0;i<width;i++)
  350.         {
  351.             *col = ((ulong)(p[1] >> 3) << 11);    // G
  352.             *col |= ((ulong)(p[0] >> 3) << 6);    // R
  353.             *col |= ((ulong)(p[2] >> 3) << 1);    // B
  354.             col++;
  355.             p+=3;
  356.         }
  357.         break;
  358.     case 24:
  359.         for (i = 0; i < width; i++)
  360.         {
  361.             *col = ((ulong)p[0] << 16);
  362.             *col |= ((ulong)p[1] << 8);
  363.             *col |= (ulong)p[2];
  364.             col++;
  365.             p+=3;
  366.         }
  367.         break;
  368.     }
  369.     cLine = p2nline((**globals).p2, &(**globals).Pixel);
  370.  
  371.     if(cLine==0)
  372.     {
  373.         // entering pass 2
  374.         (**globals).partial=gStuff->imageSize.v;
  375.     }
  376.     
  377.     if(cLine<0)
  378.     {
  379.         if(cLine==-2)
  380.             p2close((**globals).p2);
  381.         (**globals).p2 = NULL;
  382.         SetRect(&gStuff->theRect,0,0,0,0);
  383.         gStuff->data = NULL;
  384.         return;
  385.     }
  386.     gStuff->theRect.left  = 0;
  387.     gStuff->theRect.right = gStuff->imageSize.h;
  388.     gStuff->theRect.top   = cLine;
  389.     gStuff->theRect.bottom = cLine+1;
  390.     (**globals).cLine = cLine;
  391.  
  392.     if(gResult)    goto eexit;
  393.  
  394. }
  395.  
  396. /*****************************************************************************/
  397.  
  398. /* This routine will always be called if DoStart does not return an error
  399.    (even if DoContinue returns an error or the user aborts the operation).
  400.    This allows the module to perform any needed cleanup. */
  401.  
  402. void DoFinish(GHdl globals)
  403. {
  404.     if (gStuff->data)
  405.         gStuff->data = NULL;
  406. }
  407.  
  408. /*****************************************************************************/
  409.  
  410. OSErr    DoP2Error(GHdl globals)
  411. {
  412.     Str255    str;
  413.  
  414.     switch(p2errno)
  415.     {
  416.     case P2E_FEOF:
  417.         GetIndString(str, kStringID, kStrEOFErrItem);    // EOF reached
  418.         DoError(globals,str,nil);
  419.         p2errno=1;
  420.         break;
  421.     case P2E_BADFORM:
  422.         GetIndString(str, kStringID, kStrFormatErrItem);    // Format Err
  423.         DoError(globals,str,nil);
  424.         p2errno=1;
  425.         break;
  426.     default:
  427. #if 0
  428.         // MacOS Error ... Photoshop to recover this
  429.         NumToString(p2errno,str);
  430.         DoError(globals,str,nil);
  431. #endif
  432.         break;
  433.     }
  434.     return    p2errno;
  435. }
  436.  
  437. /* In dem Augenblick wurde mir klar,daァ ich einen schweren Fehler begangen habe. */
  438.  
  439. /*****************************************************************************/
  440.